package org.zp.util;

import java.security.MessageDigest;

import org.springframework.util.DigestUtils;

public class Security {
	 //盐，用于混交md5
    private static final String slat = "&%1sc*%$#@";
 
  
 
    /**
     * spring框架里面的加密方法
     *
     * @param value 需要加密的字符串
     * @return 加密后的字符串
     */
    public String getMD5(String value) {
        String base = value + "/s/" + slat;
        return DigestUtils.md5DigestAsHex(base.getBytes());
    }
 
    /**
     * 原生的加密方法
     *
     * @param value 需要加密的字符串
     * @return 加密后的字符串
     */
    public  String md5(String value) {
        try {
            value = value + slat;
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");
            messageDigest.update(value.getBytes("UTF8"));
            byte message[] = messageDigest.digest();
            String result = "";
            for (int i = 0; i < message.length; i++) {
                result += Integer.toHexString((0x000000FF & message[i]) | 0xFFFFFF00).substring(6);
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    
    public String encrypt32(String encryptStr) {
        MessageDigest md5;
        try {
            md5 = MessageDigest.getInstance("MD5");
            byte[] md5Bytes = md5.digest(encryptStr.getBytes());
            StringBuffer hexValue = new StringBuffer();
            for (int i = 0; i < md5Bytes.length; i++) {
                int val = ((int) md5Bytes[i]) & 0xff;
                if (val < 16)
                    hexValue.append("0");
                hexValue.append(Integer.toHexString(val));
            }
            encryptStr = hexValue.toString();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return encryptStr;
    }
 
    
    public String encrypt16(String encryptStr) {
        return encrypt32(encryptStr).substring(8, 24);
    }

}
